<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="relatedAggregationConfiguration" class="tb-form-panel no-border no-padding">
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" tbTruncateWithTooltip tb-hint-tooltip-icon="{{ 'calculated-fields.hint.aggregation-path-related-entities' | translate }}">
      {{ 'calculated-fields.aggregation-path-related-entities' | translate }}
    </div>
    <div class="flex gap-3 xs:flex-col" formGroupName="relation">
      <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic" hideRequiredMarker>
        <mat-label>{{ 'calculated-fields.direction' | translate }}</mat-label>
        <mat-select formControlName="direction">
          @for (direction of Directions; track direction) {
            <mat-option [value]="direction">{{ PropagationDirectionTranslations.get(direction) | translate }}</mat-option>
          }
        </mat-select>
      </mat-form-field>
      <tb-string-autocomplete [fetchOptionsFn]="fetchOptions.bind(this)"
                              class="flex-1"
                              panelWidth=""
                              additionalClass=""
                              required
                              [label]="'calculated-fields.relation-type' | translate"
                              [errorText]="'calculated-fields.hint.relation-type-required' | translate"
                              formControlName="relationType">
      </tb-string-autocomplete>
    </div>
  </div>
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" tb-hint-tooltip-icon="{{ 'calculated-fields.hint.arguments-aggregation' | translate }}">
      {{ 'calculated-fields.arguments' | translate }}
    </div>
    <tb-related-aggregation-arguments-table formControlName="arguments"
                                            [entityId]="entityId"
                                            [tenantId]="tenantId"
                                            [entityName]="entityName"
                                            [relation]="relatedAggregationConfiguration.get('relation').value"/>
  </div>
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" tb-hint-tooltip-icon="{{ 'calculated-fields.hint.metrics' | translate }}">
      {{ 'calculated-fields.metrics.metrics' | translate }}
    </div>
    <tb-calculated-field-metrics-table formControlName="metrics"
                                       [testScript]="testScript"
                                       [arguments]="arguments$ | async"
                                       [highlightRules]="argumentsHighlightRules$ | async"
                                       [editorCompleter]="argumentsEditorCompleter$ | async"
    ></tb-calculated-field-metrics-table>
    <tb-time-unit-input required
                        sameWidthInputs
                        appearance="outline"
                        subscriptSizing="dynamic"
                        labelText="{{ 'calculated-fields.deduplication-interval' | translate }}"
                        requiredText="{{ 'calculated-fields.deduplication-interval-required' | translate }}"
                        minErrorText="{{ 'calculated-fields.deduplication-interval-min' | translate: {sec: minAllowedDeduplicationIntervalInSecForCF} }}"
                        [minTime]="minAllowedDeduplicationIntervalInSecForCF"
                        formControlName="deduplicationIntervalInSec">
    </tb-time-unit-input>
  </div>
  <tb-calculate-field-output formControlName="output" [entityId]="entityId" simpleMode hiddenName>
    <div class="tb-form-row simpleMode flex-1">
      <mat-slide-toggle class="mat-slide" formControlName="useLatestTs">
        <div tb-hint-tooltip-icon="{{ 'calculated-fields.hint.use-latest-timestamp' | translate }}">
          <div translate tbTruncateWithTooltip>calculated-fields.use-latest-timestamp</div>
        </div>
      </mat-slide-toggle>
    </div>
  </tb-calculate-field-output>
</div>
